from django.contrib.auth.hashers import make_password
from django.db import models

SEX_CHOICES = (
    ('0', '女'),
    ('1', '男')
)


class User(models.Model):
    id = models.AutoField(db_column='id', primary_key=True)
    username = models.CharField(verbose_name='用户名', db_column='username', max_length=30)
    password = models.CharField(verbose_name='密码', db_column='password', max_length=100)
    name = models.CharField(verbose_name='姓名', db_column='name', max_length=20)
    sex = models.CharField(verbose_name='性别', db_column='sex', max_length=1, choices=SEX_CHOICES, default='0')
    id_card = models.CharField(verbose_name='身份证号', db_column='id_card', max_length=18)
    phone = models.CharField(verbose_name='电话', db_column='phone', max_length=11)

    def save(self, *args, **kwargs):
        self.password = make_password(self.password, None, 'pbkdf2_sha256')
        super(User, self).save(*args, **kwargs)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'user'
        verbose_name_plural = '用户'
